package com.max.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.max.common.LoginUserInfo;
import com.max.entity.UserInfo;
import com.max.service.IUserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserDetailServiceImpl implements UserDetailsService {


    @Autowired
    private IUserInfoService userInfoService;


    @Override
    public UserDetails loadUserByUsername(String loginName) throws UsernameNotFoundException {
        LambdaQueryWrapper<UserInfo> queryWrapper = new LambdaQueryWrapper<UserInfo>()
                .eq(UserInfo::getLoginName, loginName);
        UserInfo userInfo = userInfoService.getOne(queryWrapper);
        if (userInfo == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        LoginUserInfo loginUserInfo = new LoginUserInfo();
        loginUserInfo.setUsername(userInfo.getLoginName());
        loginUserInfo.setPassword(userInfo.getPwd());
        return loginUserInfo;
    }

}
